package com.goods.business.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductConverter;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.common.model.business.*;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: 赵勇镔
 * @Time: 2021/6/8 15:40
 * @Description: 物资资料实现类
 */


@Service
public class ProductServiceImpl implements ProductService {

    //物资资料mapper
    @Autowired(required = false)
    private ProductMapper productMapper;

    @Autowired(required = false)
    private ProductStockMapper productStockMapper;




    //条件查询物资资料
    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum, Integer pageSize, ParamModel2 paramModel2) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);

        Example o = new Example(Product.class);
        Example.Criteria criteria = o.createCriteria();

        String name = paramModel2.getName();
        Integer status = paramModel2.getStatus();
        Long[] categorys = paramModel2.getCategorys();


        if (name != null && !"".equals(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (status != null) {
            criteria.andLike("status", "%" + status + "%");
        }
        if (categorys != null && categorys.length == 3) {
            criteria.andLike("threeCategoryId", "%" + categorys[2] + "%");
        }


        //查询
        List<Product> products = productMapper.selectByExample(o);
        List<ProductVO> productVOS = new ArrayList<>();
        //转化vo
        for (Product product : products) {
            ProductVO productVO = ProductConverter.converterProductVO(product);
            productVOS.add(productVO);
        }
        //转化为分页对象返回
        PageInfo<Product> Info = new PageInfo<>(products);
        //前端返回需要的对象
        return new PageVO<>(Info.getTotal(), productVOS);
    }

    //添加物资资料
    @Override
    public void add(ProductVO productVO) {
        productVO.setCreateTime(new Date());
        productVO.setModifiedTime(new Date());
        productVO.setStatus(2);
        productVO.setPNum(UUID.randomUUID().toString());
        productVO.setOneCategoryId(productVO.getCategoryKeys()[0]);
        productVO.setTwoCategoryId(productVO.getCategoryKeys()[1]);
        productVO.setThreeCategoryId(productVO.getCategoryKeys()[2]);
        Product product = ProductConverter.converterProduct(productVO);
        productMapper.insert(product);
    }

    //物资资料数据回显
    @Override
    public ProductVO edit(Long id) {
        Example o = new Example(Product.class);
        o.createCriteria().andEqualTo("id", id);
        Product product = productMapper.selectOneByExample(o);
        ProductVO productVO = ProductConverter.converterProductVO(product);
        return productVO;
    }

    //物资资料保存
    @Override
    public void update(Long id, ProductVO productVO) {
        Product product = ProductConverter.converterProduct(productVO);
        product.setModifiedTime(new Date());
        Example o = new Example(Product.class);
        o.createCriteria().andEqualTo("id", id);
        productMapper.updateByExample(product, o);
    }

    //物资资料逻辑删除 0变为1
    @Override
    public void remove(Long id) {
        auditRecovery(id,productMapper,1);
    }

    //物资资料逻恢复1变为0
    @Override
    public void back(Long id) {
        auditRecovery(id,productMapper,0);
    }

    //物资资料真实删除
    @Override
    public void delete(Long id) {
        Example o = new Example(Product.class);
        o.createCriteria().andEqualTo("id", id);
        productMapper.deleteByExample(o);
    }

    //物资资料信息审核 2变0
    @Override
    public void publish(Long id) {
        auditRecovery(id,productMapper,0);
    }

    //库存信息展示
    @Override
    public List<ProductStockVO> findAllStocks(ParamModel3 paramModel3) {
        //先查询所有物资资料
        List<Product> products = productMapper.selectByExample(getExample(paramModel3));
        //前端返回需要的对象
        return getProductStockVOS(products,productStockMapper);
    }

    //库存信息展示
    @Override
    public PageVO<ProductStockVO> findProductStocks(ParamModel3 paramModel3) {
        PageHelper.startPage(paramModel3.getPageNum(),paramModel3.getPageSize());
        //先查询所有物资资料
        List<Product> products = productMapper.selectByExample(getExample(paramModel3));
        //转化为分页对象返回
        PageInfo<Product> Info = new PageInfo<>(products);
        //前端返回需要的对象
        return new PageVO<>(Info.getTotal(), getProductStockVOS(products,productStockMapper));
    }

    //获取列表模糊查询条件构造对象
    static public Example getExample (ParamModel3 paramModel3){
        Example example = new Example(Product.class);
        String name = paramModel3.getName();
        Long[] categorys = paramModel3.getCategorys();
        Example.Criteria criteria = example.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (categorys != null && categorys.length==3) {
            criteria.andLike("threeCategoryId", "%" + categorys[2] + "%");
        }
        return example;
    }

    //获取List<ProductStockVO>列表
    static public List<ProductStockVO>  getProductStockVOS(List<Product> products,ProductStockMapper productStockMapper){
        List<ProductStockVO> ProductStockVOS = new ArrayList<>();
        //根据物资资料pNum查询对应的库存数量
        for (Product product:products) {
            ProductStockVO productStockVO = new ProductStockVO();
            //把共有数据复制过去 还差库存数量
            BeanUtils.copyProperties(product,productStockVO);
            //根据pNum查询对应的库存表
            Example o2 = new Example(ProductStock.class);
            o2.createCriteria().andEqualTo("pNum",product.getPNum());
            ProductStock productStock = productStockMapper.selectOneByExample(o2);
            //把库存数值保存到要保存的VO中
            productStockVO.setStock(productStock.getStock());
            //添加VO到集合中
            ProductStockVOS.add(productStockVO);
        }
        return ProductStockVOS;
    }



    //物资信息审核和回收站恢复删除 逻辑通用 状态码不同
    static public void auditRecovery(Long id, ProductMapper productMapper,Integer statusCode) {
        Example o = new Example(Product.class);
        o.createCriteria().andEqualTo("id", id);
        Product product = productMapper.selectOneByExample(o);
        product.setModifiedTime(new Date());
        product.setStatus(statusCode);
        productMapper.updateByExample(product, o);
    }


}
